﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using NHibernate.Expression;

namespace SinsenWeb.Models
{
    [ActiveRecord]
    public class Role : EntityBase<Role>, IEntity
    {
        [PrimaryKey]
        public int Id { get; set; }

        [Property]
        public string Name { get; set; }

        private int? _memberCount ;

        public int GetMemberCount()
        {
            if (_memberCount.HasValue)
            {
                return _memberCount.Value;
            }
            else
            {

                IList<Member> members = Member.FindAll(
   DetachedCriteria.For<Member>()
     .CreateCriteria("Roles")
       .Add(Expression.Eq("Id", Id))
   );
                var count = members.Count;
                _memberCount = count;
                return count;
            }
        }
    }
}
